package app;

import java.util.ArrayList;

import app.gui.Drawable;
import app.gui.ProgressBar;

import color.Color;
import color.Rgb;

import cluster.Cluster;
import cluster.Observation;

import processing.core.PApplet;

public class Processor implements ProgressListener, Drawable{
	
	XMLParser xmlParser;
	PixelReader pixelReader;
	ClusterApp app;
	ProgressBar progressBar;
	ProgressBar totalProgressBar;
	float[] scales;
	float x, y, w, h;
	//parameters:
	String file;
	int binSize;
	int preferredSize;
	String[] files;
	float[][] ratioSet;
	float[] ratios;
	//return values:
	Cluster<Color> cluster;
	ArrayList<Color> colors;
	boolean busy = false;
	
	public Processor(ClusterApp app, float x, float y, float w, float h){
		this.app = app;
		
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		progressBar = new ProgressBar(x, y + h / 3f, w, h / 3f, new Rgb(220, 70, 50));
		totalProgressBar = new ProgressBar(x, y + h, w, h, new Rgb(20, 220, 190));
		this.xmlParser = new XMLParser();
		this.pixelReader = new PixelReader(app, progressBar);
	}
	ArrayList<String> xmlFiles;
	public void batch(String[] fs, int bin, int size, float[][] ratios){
		binSize = bin;
		preferredSize = size;
		files = fs;
		ratioSet = ratios;
		
		runThread(new Thread(){
			public void run(){
				xmlFiles = new ArrayList<String>();
				for(int i = 0; i < files.length; i++){
					colors = Color.getColorArrayFromRgb(pixelReader.getColorBins(pixelReader.getColors(app, file), preferredSize, binSize));
					totalProgressBar.val += 0.2f / (float)files.length;
					for(int k = 0; k < ratioSet.length; k++){
						totalProgressBar.val += 0.8f / (float)(files.length * ratioSet.length);
						String name = "Batch_"+files[i]+"_"+k+".xml";
						xmlFiles.add(name);
						XMLParser.writeXML(app, (new Cluster<Color>(colors, ratioSet[k], progressBar)).root, name);
					}
				}
			}
		});
	}
	
	
	
	public void getColors(String f, int bin, int size){
		binSize = bin;
		preferredSize = size;
		file = f;
		runThread(new Thread(){
			public void run(){
				colors = Color.getColorArrayFromRgb(pixelReader.getColorBins(pixelReader.getColors(app, file), preferredSize, binSize));
				processDone("getColors");
			}
		});
	}
	
	public void writeColorXml(ArrayList<Color> clr, String f){
		this.colors = clr;
		file = f;
		runThread(new Thread(){
			public void run(){
				xmlParser.writeClrXML(app, colors, file);
				processDone("writeColorXML");
			}
		});
	}

	public void cluster(ArrayList<Color> clr, float[] r){
		colors = clr;
		ratios = r;
		runThread(new Thread(){
			public void run(){
				cluster = new Cluster<Color>(colors, ratios, progressBar);
				processDone("cluster");
			}
		});
	}
	
	private void runThread(Thread thread){
		progressBar.val = 0;
		busy = true;
		thread.start();
	}
	
	
	

	public void processDone(String proc){
		busy = false;
		app.processDone(proc);
	}


	public void progress(String task, float progress) {
		progressBar.progress(task, progress);
	}
	
	public void draw(PApplet pa){
		progressBar.draw(pa);
		totalProgressBar.draw(pa);
	}
}
